<template>
  <div class="add">
    <!-- 头部 -->
    <van-sticky>
      <van-nav-bar v-if="isHome" title="黑马优购" />
      <van-nav-bar v-else title="黑马优购">
        <div class="left_arrow" @click="()=>{$router.go(-1) }" slot="left">
          <van-icon name="arrow-left" />
          <span>返回</span>
        </div>
      </van-nav-bar>
    </van-sticky>
    <!-- 新增地址栏 -->
    <van-address-edit
      :area-list="areaList"
      show-set-default
      show-search-result
      :address-info="item"
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
    />
  </div>
</template>

<script>
import areaList from "./area.js";
var num = 0;
export default {
  data() {
    return {
      areaList,
      searchResult: [],
      list: {},
      savelist: window.sessionStorage.getItem("savelist")
        ? JSON.parse(window.sessionStorage.getItem("savelist"))
        : [],
      item: {}
    };
  },
  created() {
    this.onEdit();
  },
  computed: {
    isHome: function() {
      if (this.$route.path == "/" || this.$route.path == "/home") {
        return true;
      } else {
        return false;
      }
    }
  },
  methods: {
    onSave(content) {
      console.log(content);
      num++;
      const list = {
        id: num,
        name: content.name,
        tel: content.tel,
        address: content.city + content.county + content.addressDetail,
        areaCode: content.areaCode,
        isDefault: content.isDefault
      };

      console.log(list);
      this.savelist.push(list);
      // console.log(this.savelist);

      window.sessionStorage.setItem("savelist", JSON.stringify(this.savelist));
      this.$router.push({
        path: "/address_list"
      });
    },
    onEdit() {
      const res = this.$route.query;
      console.log(res.item);
      this.item = res.item;
    }
  }
};
</script>

<style lang='less' scoped>
</style>